package com.yc.sssmpg.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.sssmpg.entity.Product;
import com.yc.sssmpg.mapper.ProductMapper;
import com.yc.sssmpg.service.IProductService;
import com.yc.framework.baseClass.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.yc.framework.baseClass.R;

import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 6521825@qq.com
 * @since 2022-11-23
 */

@Service
public class ProductServiceImpl extends BaseServiceImpl<ProductMapper, Product> implements IProductService {

    /**
     * 分页查询实体列表
     *
     * @param model 查询参数
     * @return 查询结果
     */
    @Override
    public R list(Product model) {
        IPage<Product> page = new Page<>(model.getPageNum(), model.getPageSize());
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        initCondition(wrapper);
        setParam(wrapper, model);
        IPage<Product> px = page(page, wrapper);
        return R.ok().put("data", px.getRecords()).put("total", px.getTotal());
    }

    @Override
    public R findByProductNo(Integer productNo) {
//        QueryWrapper<Product> qw=new QueryWrapper<>();
//        qw.eq(productNo!=null,"product_no",productNo);
//        List<Product> ps =list(qw);
//        return R.ok().put("isExist",ps.size()!=0);

        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(productNo != null, Product::getProductNo, productNo);
        List<Product> ps = list(lqw);
        return R.ok().put("isExist", ps.size() != 0);
    }

    /**
     * 根据主键删除实体对象，返回值封装成R，若成功则会把要删除的主键计入返回的R
     *
     * @param id 主键
     * @return 删除是否成功
     * @Override public R removeModelById(String id){
     * Product model=new Product();
     * model.setId(id);
     * return removeModelById(model);
     * }
     */
    public void setParam(QueryWrapper<Product> wrapper, Product model) {
        if (model == null) {
            return;
        }
        // wrapper.eq("deleted", false);
//        if(model.getProductNo()!=null) {
//            wrapper.eq("product_no", model.getProductNo());
//        }
        wrapper.eq(model.getProductNo() != null, "product_no", model.getProductNo())
                .like(model.getProductName() != null && model.getProductName().length() > 0, "product_name", model.getProductName())
                .eq(model.getStatus() != null, "status", model.getStatus());

    }
}
